#!/bin/bash
#
#  Copyright (c) 2025, The OpenThread Authors.
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#  3. Neither the name of the copyright holder nor the
#     names of its contributors may be used to endorse or promote products
#     derived from this software without specific prior written permission.
#
#  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#  POSSIBILITY OF SUCH DAMAGE.
#
# Run Bruno tests
#
# Note: The border router is expected to be connected to a network and not in detached state.

# fail fast on errors
set -euo pipefail

# Check DELETE collections
bru run actions/Delete_Actions_Collection.bru --env localhost --output results_$LINENO.json
bru run devices/Delete_Devices_Collection.bru --env localhost --output results_$LINENO.json
bru run diagnostics/Delete_Diagnostics_Collection.bru --env localhost --output results_$LINENO.json

##############################################################
## Discover & Get Devices
##############################################################
# POST, Bootstrap/update Device Collection as an inventory of Thread network devices
bru run actions/Post_Update_DeviceColl.bru --env localhost --output results_$LINENO.json
# we may not have gotten all responses. Repeat to have all ml-eid and the 'updated' timestamp
echo "Wait for discovery to complete ..."
sleep 11
bru run actions/Post_Update_DeviceColl.bru --env localhost --output results_$LINENO.json
# wait a few seconds and retry again. Large or lossy networks may need more POSTs to get all responses before timeout.
echo "Wait for discovery to complete ..."
sleep 11
bru run actions/Post_Update_DeviceColl.bru --env localhost --output results_$LINENO.json

# GET
# do not continue on error in device collection
bru run devices/Get_Devices_Collection.bru --env localhost --bail --output results_$LINENO.json
bru run devices/Get_Devices_Collection_SparseFields.bru --env localhost --output results_$LINENO.json

bru run devices/Get_Node.bru --env localhost --output results_$LINENO.json
bru run devices/Get_Node_json.bru --env localhost --output results_$LINENO.json
# GET a non-existing item
bru run devices/Get_DeviceItem_by_ItemId_404.bru --env localhost --output results_$LINENO.json

./setDUTdestination.sh || true # continue on error, DUT may be configured already

##############################################################
## Actions ##
##############################################################
# POST Actions
# start a Energy scan. We will request this actionId below by itemId
bru run actions/Post_Get_EnergyScan.bru --env localhost --output results_$LINENO.json
bru run actions/Post_Get_NetworkDiagnostics_AllTLVs.bru --env localhost --output results_$LINENO.json
# add a Thread Joiner Device. Note: we will not check success of joining in this script.
bru run actions/Post_Add_ThreadDevice.bru --env localhost --output results_$LINENO.json
bru run actions/Post_Reset_NetworkDiagnosticsCounter.bru --env localhost --output results_$LINENO.json
bru run actions/Post_Get_NetworkDiagnostics_Counter.bru --env localhost --output results_$LINENO.json
bru run actions/Post_Get_NetworkDiagnostics_Topology.bru --env localhost --output results_$LINENO.json
bru run actions/Post_Get_NetworkDiagnostics_Versions.bru --env localhost --output results_$LINENO.json
# POST a non-existing action
bru run actions/Post_DummyTask_422.bru --env localhost --output results_$LINENO.json

# GET Actions
bru run actions/Get_Actions_Collection.bru --env localhost --output results_$LINENO.json
#bru run actions/Get_Actions_Collection_json.bru --env localhost --output results_$LINENO.json
# GET by itemId
#bru run actions/Get_ActionItem_by_ItemId.bru --env localhost --output results_$LINENO.json  # wait until bug in bruno fixed
#bru run actions/Get_ActionItem_by_ItemId_json.bru --env localhost --output results_$LINENO.json  # wait until bug in bruno fixed
# GET a non-existing item
bru run actions/Get_ActionItem_by_ItemId_404.bru --env localhost --output results_$LINENO.json
# GET items filtered by type
bru run actions/Get_getEnergyScanTask_Items.bru --env localhost --output results_$LINENO.json
# GET items filtered by type and sparse fields
bru run actions/Get_addThreadDeviceTasks_SparseFields.bru --env localhost --output results_$LINENO.json

# give time for the tasks to complete
echo "Wait for actions to complete ..."
sleep 30
# repeat to check success of completion
bru run actions/Get_Actions_Collection_completed.bru --env localhost --output results_$LINENO.json

##############################################################
## Diagnostics
##############################################################
# GET the collection
bru run diagnostics/Get_Diagnostics_Collection.bru --env localhost --output results_$LINENO.json
# GET networkDiagnostic fields
bru run diagnostics/Get_NetworkDiagnostics_Items.bru --env localhost --output results_$LINENO.json
bru run diagnostics/Get_NetworkDiagnostics_Items_SparseFields.bru --env localhost --output results_$LINENO.json
# GET by itemId
# GET a non-existing item
bru run diagnostics/Get_DiagnosticItem_by_ItemId_404.bru --env localhost --output results_$LINENO.json
#TODO replace dummy itemid

# GET energy_scan_report fields
bru run diagnostics/Get_EnergyScanReport_Items.bru --env localhost --output results_$LINENO.json
bru run diagnostics/Get_EnergyScanReport_Items_SparseFields.bru --env localhost --output results_$LINENO.json

# Check valid options
bru run actions/Options.bru --env localhost --output results_$LINENO.json
bru run devices/Options.bru --env localhost --output results_$LINENO.json
bru run diagnostics/Options.bru --env localhost --output results_$LINENO.json
